package org.jeecgframework.web.tcdev.controller;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.tcdev.entity.TArticle;
import org.jeecgframework.web.tcdev.entity.TCompSugg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * 投诉建议控制层
 * Created by zjy on 2015/4/13.
 */
@Scope("prototype")
@Controller
@RequestMapping("/compSuggController")
public class CompSuggController extends BaseController {

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(CompSuggController.class);


    @Autowired
    private SystemService systemService;

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    @RequestMapping(params = "compSuggList")
    public ModelAndView compSuggList(HttpServletRequest request) {

        return new ModelAndView("tcdev/compSugg/compSuggList");
    }



    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     */

    @RequestMapping(params = "datagrid")
    public void datagrid(TCompSugg compSugg, HttpServletRequest request,
                         HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(TCompSugg.class, dataGrid);


        cq.add();

        // 查询条件组装器
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, compSugg, request.getParameterMap());
        this.systemService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }




    /**
     * 删除投诉建议 物理删除
     *
     * @return
     */
    @RequestMapping(params = "delCompSugg")
    @ResponseBody
    public AjaxJson delCompSugg(TCompSugg compSugg, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        compSugg = systemService.getEntity(TCompSugg.class, compSugg.getId());


        try {

            systemService.delete(compSugg);

            message = "投诉建议: " + compSugg.getContent() + "被删除成功";

        } catch (Exception e) {
            e.printStackTrace();

            message = "投诉建议: " + compSugg.getContent() + "删除失败";
        }
        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
        return j;
    }



    /**
     * 跳转到修改转视图
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(TCompSugg compSugg,
                                    HttpServletRequest req) {


        if (compSugg.getId() != null) {
            compSugg = systemService.getEntity(TCompSugg.class, compSugg.getId());

            req.setAttribute("compSugg", compSugg);

        }


        return new ModelAndView("tcdev/compSugg/compSugg");
    }



}
